package com.woniuxy.riskmanage.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.RiskRules;
import com.woniuxy.commons.entity.Staff;
import com.woniuxy.commons.rulelog.LogRequirePerms;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.riskmanage.service.RuleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author author
 * @create 2021-11-22 11:22
 */
@RestController
@RequestMapping("/rules")
public class RuleController {

    @Resource
    private RuleService ruleService;

    /**
     * 分页查询
     */
    @LogRequirePerms(module = "准入规则模块", description = "查询所有的准入规则信息")
    @SentinelResource(value = "selectPage", fallback = "fallbackSelectPage")
    @GetMapping("/selectPage")
    public ResponseResult<PageBean<RiskRules>> selectPage(RiskRules riskRules, Integer pageSize, Integer pageIndex) {
        System.out.println("查询条件:" + riskRules);
        System.out.println("pageSize:" + pageSize);
        System.out.println("pageIndex:" + pageIndex);
        PageBean<RiskRules> pageBean = new PageBean<>();
        int countRulesByPage = ruleService.selectRulesCountBylimit(riskRules);
        List<RiskRules> rules = ruleService.selectAllRules(riskRules, pageIndex, pageSize);
        pageBean.setBeanList(rules);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(countRulesByPage);
        return new ResponseResult<PageBean<RiskRules>>(200, null, "获取成功", pageBean);
    }

    /**
     * 分页查询降级方法
     *
     * @return
     */
    public ResponseResult<PageBean<RiskRules>> fallbackSelectPage() {
        return new ResponseResult<>(500, null, "系统维护中", null);
    }

    //添加规则的方法
//    @RequirePerms("user:add")
    @LogRequirePerms(module = "风控模块", description = "添加准入规则信息")
    @PostMapping("/add")
    @SentinelResource(value = "add", fallback = "fallbackaddRule")
    public ResponseResult<Staff> logaddStaff(@RequestBody RiskRules rules) {
        System.out.println(rules);
        ruleService.addRule(rules);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("添加成功");
        return responseResult;
    }

    //添加的降级方法
    public ResponseResult<Staff> fallbackaddRule() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMessage("系统维护中");
        return responseResult;
    }
}
